<?php

class PayCallback extends CI_Model
{
    private $order;
    private $notify;
    private $type;
    private $user;

    public function _call(array $data)
    {
        $this->type = $data['type'];
        $this->order = $data['data'];
        $this->notify = $data['notify'];
        $this->user = $data['member'];
        if ($this->order['module'] && method_exists($this, $this->order['module'])) {
            $this->{$this->order['module']}();
        } else {
            $this->_other();
        }
    }

    // 充值
    private function recharge()
    {
        $order = $this->models('member')->rechargeInfo($this->order['order']);
        if ($order && $order['status'] == 1) {
            $this->db->trans_start();
            $this->models('member')->modify_money($order['uid'], $order['value']);
            $this->models('member')->upRecharge(
                $order['id'],
                [
                    'status' => 2,
                    'note' => date('Ymd-H:i') . '充值到账' . $order['value'],
                    'type' => $this->type
                ]
            );
            // 添加流水
            $this->models('member/withdrawal')->addPayLog([
                'uid' => $order['uid'],
                'value' => $order['value'],
                'module' => 'recharge',
                'note' => '充值' . $order['value'],
            ]);
            $this->db->trans_complete();
        }
    }

    /**
     * 会员支付回调
     * 自己成功开通会员, 且存在上级分销用户, 按照平台返点返回上级户的账户 money 字段
     * 增加金额之后添加返点记录
     * can_money : 用户可消费金额 , 直接到账
     */
    private function vip()
    {
        $order = $this->models('member/service')->orderInfo($this->order['order'], 'vip', 'id,uid,cid,price,status');

        if ($order && $order['status'] < 1) {
            $member = $this->models('member')->uidInfo(['uid' => $order['uid']], 'uid,vip_end,parent_code');
            $info = $this->models('site/linkage')->linkInfo('recharge', 'id,type,time,rate', (int)$order['cid']);
            $time = vipEndTime($info, $member);
            $this->models('member')->upMember(
                [
                    'vip_type' => $order['cid'],
                    'vip_end' => $time
                ],
                $order['uid']
            );
            $this->models('member/service')->operateOrder($order['id'], 'vip', ['status' => 1, 'pay_time' => time()]);
            $parent = $this->models('member')->uidInfo(['randcode' => $member['parent_code']], 'uid');
            if ($info['rate'] > 0 && $parent) {
                $this->models('member')->operationMemberField(
                    $parent['uid'], 'can_money', $info['rate'] * $order['price']
                );
                $this->models('member/service')->addLog([
                    'cid' => $order['id'],
                    'price' => $order['price'],
                    'rate' => $info['rate'],
                    'money' => $info['rate'] * $order['price'],
                    'uid' => $parent['uid'],
                    'bid' => $order['uid'],
                    'time' => time()
                ]);
            }
        }
    }

    // 购买藏品回调
    private function favorite(): bool
    {
        $order = $this->models('favorite')->orderAndGood(
            $this->order['order'],
            'a.id,a.buy_uid,a.mid,a.creator_id,a.pay_status,a.team_code,b.cid,b.catid,b.title
            ,b.thumb,b.total,b.order_price,b.sn'
        );
        if ($order && $order['mid'] == 'favorite' && $order['pay_status'] <> 3) {
            $this->db->trans_start();
            // 为该用户增加卡片用于市场交易
            $this->models('favorite')->gainCard($order + ['type' => 'favorite']);
            // 更新销售件数
            $this->models('favorite')->upSellNum($order['cid'], 'favorite');
            // 接触商品的锁定状态
            $this->models('favorite')->operateLock($order['cid'], 0);
            // 更新订单状态
            $this->models('favorite')->operateOrder(
                $order['id'],
                [
                    'order_status' => 3,
                    'pay_status' => 3,
                    'pay_time' => time(),
                    'pay_type' => $this->type,
                    'out_trade_no' => $this->notify['out_trade_no'] ?: ''
                ]
            );
            //删除订单队列
            $this->models('favorite')->delQueue($order['id']);
            if ($this->type <> 'given') {
                // 更新用户消费金额
                $this->models('member')->operationMemberField($order['buy_uid'], 'spend', $order['order_price']);
                // 给创作者增加佣金
                $this->models('issuer')->operateField($order['creator_id'], $order['order_price']);
                // 增加流水记录
                $this->models('member/withdrawal')->addPayLog([
                    'uid' => $order['buy_uid'],
                    'value' => -$order['order_price'],
                    'module' => 'favorite',
                    'note' => '购买藏品' . $order['order_price'],
                ]);
                ### 存在团队返点
                if ($order['team_code']) {
                    $this->addMoney($order);
                }
            }

            $this->db->trans_complete();
            return true;
        }
        return false;
    }

    // 盲盒回调
    private function box(): bool
    {
        $order = $this->models('favorite')->orderAndGood(
            $this->order['order'],
            'a.id,a.buy_uid,a.mid,a.pay_status,b.cid,b.order_price'
        );
        if ($order && $order['mid'] == 'box' && $order['pay_status'] <> 3) {
            $this->db->trans_start();
            // 更新销售件数
            $this->models('favorite')->upSellNum($order['cid'], 'box');
            $this->models('member')->operationMemberField($order['buy_uid'], 'spend', $order['order_price'], '+');
            // 更新订单状态
            $this->models('favorite')->operateOrder(
                $order['id'],
                [
                    'pay_status' => 3,
                    'pay_time' => time(),
                    'pay_type' => $this->type,
                    'out_trade_no' => $this->notify['out_trade_no'] ?: ''
                ]
            );
            $this->models('member/withdrawal')->addPayLog([
                'uid' => $order['buy_uid'],
                'value' => -$order['order_price'],
                'module' => 'favorite',
                'note' => '购买盲盒' . $order['order_price'],
            ]);
            $this->db->trans_complete();
            return true;
        }
    }

    // 市场支付完成回调
    private function market()
    {
        $market = $this->models('market')->marketOrderInfo(
            $this->order['order'],
            'id,cid,market_id,price,num,total,order_status,pay_status,cid,catid,my_id,buy_uid,title,thumb'
        );
        if ($market['order_status'] == 1 && $market['pay_status'] == 0) {
            // 更新订单状态
            $this->models('market')->operateOrder(
                $market['id'],
                [
                    'order_status' => 3,
                    'pay_status' => 3,
                    'pay_time' => time(),
                    'pay_type' => $this->type,
                    'out_trade_no' => $this->notify['out_trade_no'] ?: ''
                ]
            );
            // 更新用户消费金额
            $this->models('member')->operationMemberField($market['buy_uid'], 'spend', $market['price'], '+');
            // 将市场中对应信息更新购买者和购买时间
            $this->models('market')->operateGood(
                $market['market_id'],
                ['buy_uid' => $market['buy_uid'], 'buy_time' => time()]
            );
            // 将我的藏品标记为已售出状态
            $this->models('favorite')->operateCard($market['my_id'], 3);
            // 为该用户增加卡片用于市场交易
            $data = [
                'buy_uid' => $market['buy_uid'],
                'id' => $market['id'],
                'cid' => $market['cid'],
                'catid' => $market['catid'],
                'title' => $market['title'],
                'sn' => $market['num'],
                'total' => $market['total'],
                'thumb' => $market['thumb'],
                'order_price' => $market['price'],
                'type' => 'market'
            ];
            $this->models('favorite')->gainCard($data);
            //删除订单队列
            $this->models('favorite')->delQueue($market['id']);
            // 增加流水日志
            $this->models('member/withdrawal')->addPayLog([
                'uid' => $market['buy_uid'],
                'value' => -$market['price'],
                'module' => 'market',
                'note' => '市场购买' . $market['price'],
            ]);
        }
    }

    // 增加佣金
    protected function addMoney($data)
    {
        $parent = $this->models('member')->uidInfo(['randcode' => $data['team_code']], 'uid');
        if ($parent) {
            $value = $data['order_price'] * 0.001;
            return $this->models('member/withdrawal')->addPayLog([
                'uid' => $data['buy_uid'],
                'value' => -$value,
                'module' => 'fee',
                'note' => '返点' . $value,
                'pid' => $parent['uid']
            ]);
            $this->models('member')->operationMemberField($parent['uid'], 'money', $value);
        }
        return true;
    }

}
